package com.bcn.login_mybatis_demo.controller;

import com.bcn.login_mybatis_demo.dto.RoleDTO;
import com.bcn.login_mybatis_demo.pojo.Role;
import com.bcn.login_mybatis_demo.pojo.UserDTO;
import com.bcn.login_mybatis_demo.result.ListResult;
import com.bcn.login_mybatis_demo.service.RoleService;
import com.bcn.login_mybatis_demo.service.serviceImpl.RoleServiceImpl;
import com.bcn.login_mybatis_demo.util.Result;
import com.bcn.login_mybatis_demo.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleService roleService;
    @Autowired
    private RoleServiceImpl roleServiceImpl;

    @PostMapping("/roleList")
    public Result roleList(@RequestBody(required = false) RoleDTO roleDTO) {
        if (roleDTO == null) {
            roleDTO = new RoleDTO();
        }
        List<Role> data = roleService.getRoleList(roleDTO);
        long total = roleService.getRoleCount(roleDTO);

        ListResult listResult = new ListResult(data, total);
        return ResultUtil.success(listResult);
    }
    @PostMapping("/createRole")
    public Result createRole(@RequestBody Role role) {
        if ("".equals(role.getName())) {
            return ResultUtil.error("角色名称不能为空");
        }else {
            String msg = roleServiceImpl.createRole(role);
            if(("SUCCESS").equals(msg)){
                return ResultUtil.success(role);
            }else{
                return ResultUtil.error(msg);
            }
        }
    }
    @PostMapping("/delRole")
    public Result deleteRole(@RequestBody RoleDTO roleDTO) {
        if (roleDTO == null || roleDTO.getId() == null) {
            return ResultUtil.error("角色ID不能为空");
        }
        String msg = roleServiceImpl.deleteRoleById(roleDTO.getId());

        if ("SUCCESS".equals(msg)) {
            return ResultUtil.success("角色删除成功");
        } else {
            return ResultUtil.error("删除失败: " + msg);
        }
    }

    @PostMapping("/updateRole")
    public Result updateRole(@RequestBody RoleDTO roleDTO) {
        if (roleDTO == null || roleDTO.getId() == null) {
            return ResultUtil.error("角色ID不能为空");
        }
        Role role = roleService.getRoleByID(roleDTO.getId());
        if (role == null) {
            return ResultUtil.error("角色不存在");
        }
        if(roleDTO.getName() != null || roleDTO.getDescription() != null){
            role.setName(roleDTO.getName());
            role.setDescription(roleDTO.getDescription());

            String msg = roleService.updateRole(role);
            if (("SUCCESS").equals(msg)) {
                role = roleService.getRoleByID(roleDTO.getId());
                return ResultUtil.success(role);
            }
            return ResultUtil.error(msg);
        }
        return ResultUtil.success(role);
    }

    @PostMapping("/roleDetail")
    public Result roleDetail(@RequestBody RoleDTO roleDTO) {
        if (roleDTO == null || roleDTO.getId() == null) {
            return ResultUtil.error("角色ID不能为空");
        }
        Role role = roleService.getRoleByID(roleDTO.getId());
        if (role == null) {
            return ResultUtil.error("角色不存在");
        }
        return ResultUtil.success(role);
    }
}
